var crypto = require('crypto');
var key = require('../config').config.cryptoKey;
// var cipher = crypto.createCipher('aes-256-cbc', key);
// var decipher = crypto.createDecipher('aes-256-cbc', key);


function verifyEmail(email) {
	var sReg = /[_a-zA-Z\d\-\.]+@[_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+)+$/;
	if(!sReg.test(email)) {
		return false;
	}
	return true;
}

function md5(value) {
	return crypto.createHash('md5').update(value).digest("hex");
}
//+
function toEncrypt(value) {
	if(!value) return '';
	var cipher = crypto.createCipher('aes-256-cbc', key);
	var msg = cipher.update(value, 'utf8', 'hex');
	msg += cipher.final('hex');
	return msg;
}
//-
function toDecrypt(value) {
	if(!value) return '';
	var decipher = crypto.createDecipher('aes-256-cbc', key);
	var msg = decipher.update(value, 'hex', 'utf8');
	msg += decipher.final('utf8');
	return msg;
}
exports.verifyEmail = verifyEmail;
exports.md5 = md5;
exports.toEncrypt = toEncrypt;
exports.toDecrypt = toDecrypt;